var rewire = require("rewire")
const moment = require('moment')
const R = require('ramda')
const combinator = rewire('../common/combinator.cjs')
const udi = rewire('../common/udi.cjs')
const utils = rewire('../common/utils.cjs')
const staticData = rewire('../common/staticData.cjs')
const driver = rewire('../common/webdriver.cjs')



async function main(sdate, edate) {
  await combinator.mapIncreaseUntil(
    udi.grapServer,
    sdate,
    (date) => {
      return moment(date).subtract(1, 'days').format('YYYY-MM-DD')
    },
    (date) => {
      return date === edate
    }
  )
  console.log('跑完了')
}


main('2020-05-12', '2015-01-02')



main('2015-02-14', '2015-01-02')

// 数据肯定不够多
// 说到底还得到那个网站去刮下来呢

async function test() {
  const resp = await udi.apiShareGet('2021-09-01', 1)
  console.log(resp)
}
test()




/**
** 浏览器刮取方案
**
**
**/

async function grapMain() {
  await grapTaskCountry()
  await grapTaskOther()
  console.log('OK')
}

async function grapTaskCountry() {
  const keywords = getCountryKeyword()
  await utils.parallelMapCatch(
    1,
    async function(key) { await clientSearchAll(client, key, savePageData) },
    keywords
  )
}

async function grapTaskOther() {
  const pList = staticData.getProvinceList()
  await utils.parallelMapCatch(
    1,
    handleByProvince,
    pList
  )
}

async function handleByProvince(province) {
  const resp = await clientSearchPage(client, staticData.provinceShort(province[1]), 1)
  if (resp.maxPage > 499) {
    const cList = staticData.getCityShortList(province)
    await utils.parallelMapCatch(
      1,
      handleByCity,
      cList
    )
  }
  else {
    await clientSearchAll(client, staticData.provinceShort(province), savePageData)
  }
}

async function handleByCity(cityShort) {
  const resp = await clientSearchPage(client, cityShort, 1)
  if (resp.maxPage > 499) {
    const citykeyList = R.flatten(R.map(getHeadList, getCityLevelTypes(cityShort)))
    const provinceKeyList = R.flatten(
      R.map(
        getHeadList,
        getProvinceLevelTypes(staticData.provinceShort(province[1]))
      )
    )
    await utils.parallelMapCatch(
      1,
      async function(keyword) {
        await clientSearchAll(client, keyword, savePageData)
      },
      R.concat(citykeyList, provinceKeyList)
    )
  }
  else {
    await clientSearchAll(client, cityShort, savePageData)
  }
}



///////////// 关键词函数

function getCountryKeyword() {
  return [].concat(...R.map(getHeadList, ["国械注准", '国械备', '国械注进', '国械注许']))
}

function getCityLevelTypes(cityShort) {
  return R.map(R.partial(R.concat, [cityShort]), ['械备'])
}

function getProvinceLevelTypes(provinceShort) {
  return [`${provinceShort}械注准`, `${provinceShort}械备`]
}

function getHeadList(key) {
  return R.map(year => key + year, R.range(2008, 2023))
}









///////////// 浏览器函数

let client = null

async function initNmpaClient() {
  if (!client) {
    client = await driver.createWebDriver('nmpa')
  }
  await driver.driverGoto(client, 'https://www.nmpa.gov.cn/datasearch/home-index.html?79QlcAyHig6m=1642906961785#category=ylqx')
  const elems = await driver.elemFindList(client, '.pc-max.el-row .el-col')
  await driver.elemClick(elems[0])
}


async function clientSearchAll(client, keyword, savePageData) {

}

async function clientSearchPage(client, keyword, page) {
}


///////////// 数据库函数
async function savePageData(data) {
  await db.insertAllNotIn(state.db, 'nmpa', 'regist_num', data)
}

const client = await initNmpaClient()
